package com.uuah.server.config.spring.processors;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import com.uuah.server.api.component.model.IModel;
import com.uuah.server.api.component.service.IService;
import com.uuah.server.api.transformer.IBaseTransformer;
import com.uuah.server.api.transport.IConnector;

public class UuahObjectNameProcessor implements BeanPostProcessor {
	private boolean overwrite = false;

	public Object postProcessBeforeInitialization(Object o, String s)
			throws BeansException {

		if (o instanceof IConnector) {
			if (((IConnector) o).getName() == null || overwrite) {
				((IConnector) o).setName(s);
			}
		} else if (o instanceof IBaseTransformer) {
			if (((IBaseTransformer) o).getName() == null || overwrite) {
				((IBaseTransformer) o).setName(s);
			}
		} else if (o instanceof IService) {
			if (((IService) o).getName() == null || overwrite) {
				((IService) o).setName(s);
			}
		} else if (o instanceof IModel) {
			if (((IModel) o).getName() == null || overwrite) {
				((IModel) o).setName(s);
			}
		}
		return o;
	}

	public Object postProcessAfterInitialization(Object o, String s)
			throws BeansException {
		return o;
	}

	public boolean isOverwrite() {
		return overwrite;
	}

	public void setOverwrite(boolean overwrite) {
		this.overwrite = overwrite;
	}

}
